import { http } from "@/utils/http";
import type { Base, BaseTree, Page } from "@/api/types";

export type SimpleDict = {
  label?: string;
  code?: string;
  value?: string;
  color?: string;
};

export type Dict = SimpleDict &
  BaseTree & {
    description?: string;
  };

export const dictOptions = (code: string) => {
  return http.request<Array<SimpleDict>>("get", "/dict/options", { code });
};

export const dictTranslate = (code: string, value: any) => {
  return http.request<SimpleDict>("get", "/dict/translate", {
    code,
    value
  });
};

export const checkDictCode = (code: string) => {
  return http.request<boolean>("get", "/dict/check", { code });
};

export const getDictList = (params?: Dict) => {
  return http.request<Array<Dict>>("get", "/dict", params);
};
export const addDict = (params?: Dict) => {
  return http.request<boolean>("post", "/dict", params);
};
export const updateDict = (params?: Dict) => {
  return http.request<boolean>("put", "/dict", params);
};
export const deleteDict = (id?: string) => {
  return http.request<boolean>("delete", "/dict", { id: id });
};

export const clearDictCache = () => {
  return http.request("delete", "/dict/clear_cache");
};

export type User = Base & {
  name?: string;
  username?: string;
  password?: string;
  status?: string;
};

/** 获取用户管理列表 */
export const getUserList = (params?: User) => {
  return http.request<Page<User>>("get", "/user", params);
};
export const resetPassword = (id: string) => {
  return http.request<string>("post", `/user/reset_password/${id}`);
};

export const bindRole = (userId: string, role: Array<string>) => {
  return http.request<boolean>("post", "/user/bind_role", {
    id: userId,
    roleIds: role
  });
};

export type Role = Base & {
  code?: string;
  name?: string;
  state?: boolean;
  description?: string;
};
/** 用户管理-获取所有角色列表 */
export const getRoleList = (params?: Role) => {
  return http.request<Page<Role>>("get", "/role", params);
};
export const addRole = (params?: Role) => {
  return http.request<boolean>("post", "/role", params);
};
export const updateRole = (params?: Role) => {
  return http.request<boolean>("put", "/role", params);
};
export const deleteRole = (id?: string) => {
  return http.request<boolean>("delete", "/role", { id: id });
};

export const bindPermission = (roleId: string, permission: Array<string>) => {
  return http.request<boolean>("post", "/role/bind_permission", {
    id: roleId,
    permissionIds: permission
  });
};

export const getUserRoles = (userId: string) => {
  return http.request<Array<string>>("get", `/role/user/${userId}`);
};

export type Permission = Base & {
  name?: string;
  code?: string;
  state?: boolean;
  description?: string;
  group?: string;
};

export const getPermissionList = (params?: Permission) => {
  return http.request<Page<Permission>>("get", "/permission", params);
};
export const addPermission = (params?: Permission) => {
  return http.request<boolean>("post", "/permission", params);
};
export const updatePermission = (params?: Permission) => {
  return http.request<boolean>("put", "/permission", params);
};
export const deletePermission = (id?: string) => {
  return http.request<boolean>("delete", "/permission", { id: id });
};
export const getRolePermissions = (roleId: string) => {
  return http.request<Array<string>>("get", `/permission/role/${roleId}`);
};
export const bindMenu = (permissionId: string, menu: Array<string>) => {
  return http.request<boolean>("post", "/permission/bind_menu", {
    id: permissionId,
    menuIds: menu
  });
};
export const getPermissionGroups = () => {
  return http.request<Array<string>>("get", "/permission/group");
};

export type Menu = BaseTree & {
  path?: string;
  component?: string;
  title?: string;
  name?: string;
  type?: string;
  icon?: string;
  frameSrc?: string;
  showLink?: boolean;
  keepAlive?: boolean;
};

export const getMenuList = (params?: Menu) => {
  return http.request<Array<Menu>>("get", "/menu", params);
};
export const addMenu = (params?: Menu) => {
  return http.request<boolean>("post", "/menu", params);
};
export const updateMenu = (params?: Menu) => {
  return http.request<boolean>("put", "/menu", params);
};
export const deleteMenu = (id?: string) => {
  return http.request<boolean>("delete", "/menu", { id: id });
};

export const getPermissionMenu = (permissionId: string) => {
  return http.request<Array<string>>("get", `/menu/permission/${permissionId}`);
};

export type Config = Base & {
  key?: string;
  value?: string;
  desc?: string;
  type?: string;
  dict?: string;
  state?: boolean;
};

export const getAsyncConfig = () => {
  return http.request<object>("get", "/config/serverConfig");
};
export const checkConfigKey = (key: string) => {
  return http.request<boolean>("get", "/config/check", { key });
};
export const getConfigList = (params?: Config) => {
  return http.request<Array<Config>>("get", "/config", params);
};
export const addConfig = (params: Config) => {
  return http.request<boolean>("post", "/config", params);
};
export const updateConfig = (params: Config) => {
  return http.request<boolean>("put", "/config", params);
};
export const deleteConfig = (id?: string) => {
  return http.request<boolean>("delete", "/config", { id: id });
};
export const clearConfigCache = () => {
  return http.request("delete", "/config/clear_cache");
};

export type Log = Base & {
  busType?: string;
  title?: string;
  status?: string;
  remoteAddr?: string;
  ipRegion?: string;
  userAgent?: string;
  requestUri?: string;
  requestMethod?: string;
  logMethod?: string;
  logParams?: string;
  returnValue?: string;
  logException?: string;
  startTime?: string;
  endTime?: string;
  cost?: string;
  os?: string;
  browser?: string;
};

export const getLogList = (params?: Log) => {
  return http.request<Page<Log>>("get", "/log", params);
};
